﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kardex_Entity
{
    public class SucursalDA
    {
        bd_kardexEntities context;
        public SucursalDA()
        {
            context = new bd_kardexEntities();
        }

        public List<sucursal> GetListAllByIdPerson(long IdPerson)
        {
            List<sucursal> ListSucursal = null;
            try
            {
                var q = from s in context.sucursal
                        where s.IdPerson == IdPerson
                        orderby s.IdPerson
                        select s;
                ListSucursal = q.ToList();

                return ListSucursal;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        public sucursal GetObjByIdSucursal(int IdSucursal)
        {
            try
            {
                var q = from s in context.sucursal
                        where s.IdSucursal == IdSucursal
                        select s;
                sucursal objPerson = (sucursal)q.FirstOrDefault();

                return objPerson;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        public sucursal Save(sucursal NewSucursal)
        {
            try
            {
                using (bd_kardexEntities conex=new bd_kardexEntities())
                {
                    conex.sucursal.AddObject(NewSucursal);
                    conex.SaveChanges();
                }
                //context.sucursal.AddObject(NewSucursal);
                //context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return NewSucursal;
        }

        public bool Udate(sucursal SucursalModif)
        {
            bool ReturnResult = false;
            try
            {
                context.sucursal.Attach(SucursalModif);
                context.ObjectStateManager.ChangeObjectState(SucursalModif, System.Data.EntityState.Modified);
                context.SaveChanges();

                ReturnResult = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return ReturnResult;
        }

        public bool Delete(sucursal SucursalDelete)
        {
            bool ReturnResult = false;
            try
            {
                context.sucursal.Attach(SucursalDelete);
                context.DeleteObject(SucursalDelete);
                context.SaveChanges();
                ReturnResult = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return ReturnResult;
        }
    }
}

